package com.vieewer.btdemo.view.phonecall

import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.WindowManager
import com.vieewer.btdemo.R
import com.vieewer.btdemo.base.BaseActivity
import kotlinx.android.synthetic.main.activity_bluetooth_call.*

class BluetoothCallActivity : BaseActivity() {

    var mContactName : String ? = ""
    var mContactPhoneNumber : String ? = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_bluetooth_call)

        window.addFlags(
              WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON or
                    WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

        mContactName = intent.getStringExtra("contactName")
        mContactPhoneNumber = intent.getStringExtra("contactPhoneNumber")
        initViews()
    }


    private fun initViews() {

/*        callToggleButton.setOnEvent(object : CallToggleButton.OnEventListener {
            override fun RightEvent() {
                Log.i("simple", "滑动到最右")
                Toast.makeText(this@BluetoothCallActivity, "滑动到最右", Toast.LENGTH_SHORT).show()
            }

            override fun LeftEvent() {
                Log.i("simple", "滑动到最左")
                Toast.makeText(this@BluetoothCallActivity, "滑动到最左", Toast.LENGTH_SHORT).show()
            }

        })*/

        txt_terminate_call.setOnClickListener {
            terminatePhoneCall()
        }

        /* txt_accept_call.setOnClickListener {
            pickUpPhoneCall()
        }*/

        if (mContactName != null && mContactName != "") {
            txt_contact_person.text = mContactName
        }

        if (mContactPhoneNumber != null && mContactPhoneNumber != "") {
            txt_phone_num.text = mContactPhoneNumber
        }



    }

    private fun terminatePhoneCall() {
        BluetoothManager.getInstance().terminateCall(object : BluetoothManager.OnCallResultListener {

            override fun onBluetoothIsClosed() {
                Log.i("yjj", "terminateCall onBluetoothIsClosed")
            }

            override fun onDeviceIsEmpty() {
                Log.i("yjj", "terminateCall onDeviceIsEmpty")
            }

            override fun onPhoneIsInValid() {
                Log.i("yjj", "terminateCall onPhoneIsInValid")
            }

            override fun onError(errorMsg: String?) {
                Log.i("yjj", "terminateCall onError>>>$errorMsg")
            }

        })
    }

    private fun dial() {
        var phoneNum = edt_input_phone_num.text.toString().trim()
        Log.i("yjj", "phoneNum >>>$phoneNum")

        BluetoothManager.getInstance().dial(phoneNum, object : BluetoothManager.OnCallResultListener {

            override fun onBluetoothIsClosed() {
                Log.i("yjj", "onBluetoothIsClosed")
            }

            override fun onDeviceIsEmpty() {
                Log.i("yjj", "onDeviceIsEmpty")
            }

            override fun onPhoneIsInValid() {
                Log.i("yjj", "onPhoneIsInValid")
            }

            override fun onError(errorMsg: String?) {
                Log.i("yjj", "onError>>>$errorMsg")
            }

        })
    }

    private fun rejectCall() {
        BluetoothManager.getInstance().rejectCall(object : BluetoothManager.OnCallResultListener {

            override fun onBluetoothIsClosed() {
                Log.i("yjj", "rejectCall onBluetoothIsClosed")
            }

            override fun onDeviceIsEmpty() {
                Log.i("yjj", "rejectCall onDeviceIsEmpty")
            }

            override fun onPhoneIsInValid() {
                Log.i("yjj", "rejectCall onPhoneIsInValid")
            }

            override fun onError(errorMsg: String?) {
                Log.i("yjj", "rejectCall onError>>>$errorMsg")
            }

        })
    }

    private fun acceptCall() {
        BluetoothManager.getInstance().acceptCall(object : BluetoothManager.OnCallResultListener {

            override fun onBluetoothIsClosed() {
                Log.i("yjj", "acceptCall onBluetoothIsClosed")
            }

            override fun onDeviceIsEmpty() {
                Log.i("yjj", "acceptCall onDeviceIsEmpty")
            }

            override fun onPhoneIsInValid() {
                Log.i("yjj", "acceptCall onPhoneIsInValid")
            }

            override fun onError(errorMsg: String?) {
                Log.i("yjj", "acceptCall onError>>>$errorMsg")
            }

        })

    }


    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {

        Log.i("yjj", "BluetoothCallActivity onKeyUp >>> keyCode" + keyCode + ", action =" + event!!.action)

        when(keyCode) {

            119 -> {  //Fn按键对应键值为119

            }

            22 -> {   //往前滑动触控板
                rejectPhoneCall()
            }


            21 -> {    //往后滑动触控板
                pickUpPhoneCall()
            }

        }

        return super.onKeyUp(keyCode, event)

    }

    private fun rejectPhoneCall() {
        callToggleButton.moveToRight()
        rejectCall()
        this.finish()
    }


    private fun pickUpPhoneCall() {
        callToggleButton.moveToLeft()
        acceptCall()
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        Log.i("yjj", "BluetoothCallActivity onKeyDown >>> keyCode$keyCode")
        if (keyCode == 66 ) {
   
        }
        return super.onKeyDown(keyCode, event)

    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        Log.i("yjj", "BluetoothCallActivity onTouchEvent >>> event.action ${event.action}")
        when (event.action) {

            MotionEvent.ACTION_DOWN -> {

            }

            MotionEvent.ACTION_UP -> {

            }
        }

        return true
    }







}